package cn.hahah.module.blog.service.userfriend;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.hahah.module.blog.controller.admin.userfriend.vo.*;
import cn.hahah.module.blog.dal.dataobject.userfriend.UserFriendDO;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.util.object.BeanUtils;

import cn.hahah.module.blog.dal.mysql.userfriend.UserFriendMapper;

import static cn.hahah.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hahah.module.blog.enums.ErrorCodeConstants.*;

/**
 * 用户好友 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserFriendServiceImpl implements UserFriendService {

    @Resource
    private UserFriendMapper userFriendMapper;

    @Override
    public Long createUserFriend(UserFriendSaveReqVO createReqVO) {
        // 插入
        UserFriendDO userFriend = BeanUtils.toBean(createReqVO, UserFriendDO.class);
        userFriendMapper.insert(userFriend);
        // 返回
        return userFriend.getId();
    }

    @Override
    public void updateUserFriend(UserFriendSaveReqVO updateReqVO) {
        // 校验存在
        validateUserFriendExists(updateReqVO.getId());
        // 更新
        UserFriendDO updateObj = BeanUtils.toBean(updateReqVO, UserFriendDO.class);
        userFriendMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserFriend(Long id) {
        // 校验存在
        validateUserFriendExists(id);
        // 删除
        userFriendMapper.deleteById(id);
    }

    private void validateUserFriendExists(Long id) {
        if (userFriendMapper.selectById(id) == null) {
            throw exception(USER_FRIEND_NOT_EXISTS);
        }
    }

    @Override
    public UserFriendDO getUserFriend(Long id) {
        return userFriendMapper.selectById(id);
    }

    @Override
    public PageResult<UserFriendDO> getUserFriendPage(UserFriendPageReqVO pageReqVO) {
        return userFriendMapper.selectPage(pageReqVO);
    }

}